<template>
  <a-modal
    :confirmLoading="loading"
    :visible="visible"
    destroyOnClose
    title="发货"
    width="40%"
    @cancel="
      () => {
        this.visible = false
      }
    "
    @ok="handleSubmit"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="快递单号" prop="TrackingNumber">
          <a-input v-model="entity.TrackingNumber" autocomplete="off" placeholder="快递单号"/>
        </a-form-model-item>
        <a-form-model-item label="附件上传" prop="File">
          <CUploadFile
            v-model="entity.File"
            :bt="10"
            :fileUrl="'/SampleManagement/SPL_SampleApplyMain/UploadFileByForm'"
            :maxCount="5"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import CUploadFile from '@/components/CUploadFile/CUploadFile.vue'

export default {
  components: {
    CUploadFile
  },
  props: {
    parentObj: Object
  },
  data () {
    return {
      id: '',
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: { TrackingNumber: [{ required: true, message: '必填', trigger: 'blur' }] },
      title: ''
    }
  },
  methods: {
    openForm (id) {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.id = id
    },
    createAttachmentsList () {
      if (this.entity.File && this.entity.File.length > 0) {
        const attachmentsList = []
        this.entity.File.forEach((file) => {
          attachmentsList.push({ RelationId: this.id, FileURL: file })
        })
        return attachmentsList
      } else {
        return []
      }
    },
    handleSubmit () {
      this.$http.post('/SampleManagement/SPL_SampleApplyMain/UpdateDataByCondition', {
        Conditions: [
          { Condition: 'TrackingNumber', Keyword: this.entity.TrackingNumber },
          { Condition: 'DeliverStatus', Keyword: 2 }
        ],
        ids: [this.id]
      }).then(resJson => {
        if (resJson.Success) {
          this.$message.success('操作成功!')
          this.visible = false
          this.parentObj.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
      this.$http.post('/Base_BasicData/Base_AttachmentsList/SaveDataByList', this.createAttachmentsList())
        .then(resJson => {
          if (resJson.Success) {
            this.visible = false
            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
    }
  }
}
</script>
<style>
.ant-upload {
  text-align: center !important;
}

.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}

.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
